package ch6;

import java.sql.*;

public class jdbcTest {
    public static void main(String[] args) {
        try {
            Class.forName ("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/smbms";
            String user = "root";
            String password = "123456";
            com.mysql.jdbc.Connection conn= null;
            try {
                conn = (com.mysql.jdbc.Connection) DriverManager.getConnection (url,user,password);
            } catch (SQLException throwables) {
                throwables.printStackTrace ();
            }
            try {
                System.out.println (conn.getMetaData ());
            } catch (SQLException throwables) {
                throwables.printStackTrace ();
            }
            String sql1 = "SELECT * FROM smbms_user WHERE id=1";
            String sql2 = "SELECT * FROM smbms_user WHERE id=?";
            Statement st = conn.createStatement ();
            PreparedStatement prep = conn.prepareStatement (sql2);
            prep.setInt (1,2);
            ResultSet rst1 = st.executeQuery (sql1);
            ResultSet rst2 = prep.executeQuery ();

            while (rst1.next ()){
                int id1 = rst1.getInt ("id");
                String userCode1 = rst1.getString ("userCode");
                String userName1 = rst1.getString ("userName");
                System.out.println ("id="+id1+"userCode="+userCode1+"userName="+userName1);
            }
            while (rst2.next ()){
                int id2 = rst2.getInt ("id");
                String userCode2 = rst2.getString ("userCode");
                String userName2 = rst2.getString ("userName");
                System.out.println ("id="+id2+"userCode="+userCode2+"userName="+userName2);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace ();
        }
    }
}
